<?php
namespace app\admin\controller;

use think\Controller;

class Category extends Base
{
    private $obj = '';

    public function _initialize()
    {
        $this->obj = model('Category');
    }

    public function index()
    {
        $parentId = input('get.parent_id', 0, 'intval');

        $categorys = $this->obj->getFirstCategorys($parentId);
        return $this->fetch('', [
            'categorys' => $categorys,
        ]);
    }

    public function add()
    {
        $categorys = $this->obj->getNormalFirstCategory();
        return $this->fetch('', [
            'categorys' => $categorys,
        ]);
    }

    public function save()
    {
        // 做下严格判断
        if (!request()->isPost()) {
            $this->error('请求不合法');
        }
        $data = input('post.');
        $validate = validate('Category');
        if (!$validate->scene('add')->check($data)) {
            $this->error($validate->getError());
        }
        // 存在id走更新操作
        if (!empty($data['id'])) {
            return $this->update($data);
        }
        // 把数据提交给模型
        $res = $this->obj->add($data);
        if ($res) {
            $this->success('分类添加成功', 'category/add');
        } else {
            $this->error('分类添加失败');
        }
    }

    /**
     * 编辑页面
     */
    public function edit()
    {
        $id = input('get.id');
        if ($id < 0) {
            $this->error('参数不合法');
        }
        $category = $this->obj->get($id);
        $categorys = $this->obj->getNormalFirstCategory();
        return $this->fetch('', [
            'categorys' => $categorys,
            'category' => $category,
        ]);
    }

    public function update($data)
    {
        $res = $this->obj->save($data, ['id' => intval($data['id'])]);
        if ($res) {
            $this->success('更新成功', 'category/index');
        } else {
            $this->error('更新失败');
        }
    }

    // 排序逻辑
    public function listorder($id, $listorder)
    {
        $res = $this->obj->save(['listorder' => $listorder], ['id' => $id]);
        if ($res) {
            $this->result($_SERVER['HTTP_REFERER'], 1, '更新成功');
        } else {
            $this->result($_SERVER['HTTP_REFERER'], 0, '更新失败');
        }
    }

    // 修改状态
    /*public function status()
    {
        $data = input('get.');
        $validate = validate('Category');
        if (!$validate->scene('status')->check($data)) {
            $this->error($validate->getError());
        }
        $res = $this->obj->save(['status' => $data['status']], ['id' => $data['id']]);
        if($res){
            $this->success('状态修改成功');
        }else{
            $this->error('状态修改成功');
        }
    }*/
}
